from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebKitWidgets import *
from PyQt5.QtWebKit import QWebSettings

import numpy as np
import cv2
from QtExtend.EQLabel import *
from Lib.CvCamera import *
from Lib.Utils import *
from JSInterface.SysControl import *

import cgitb 
import threading
import sys
import datetime
import time
import os


#窗体大小
windowWidth = 1024
windowHeight = 600

#主窗体
class MainWindow(QMainWindow):
   
    def __init__(self):
       super().__init__()
       
       self.setWindowTitle("烟花爆竹75℃")
       self.resize(windowWidth,windowHeight)
       #使窗体总在最前端
       #self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
       
       #创建WebView容器
       self.browser = QWebView()
       #控制WebView容器所有连接本窗口打开
       self.browser.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
       self.browser.page().linkClicked.connect(lambda url:self.browser.load(url))
       #添加浏览器到窗口中
       self.setCentralWidget(self.browser)
       self.browser.settings().setAttribute(QWebSettings.LocalContentCanAccessRemoteUrls,True)
       self.browser.settings().setAttribute(QWebSettings.DeveloperExtrasEnabled,True)
       #QApplication.desktop().screenGeometry().width()

       # #开启摄像头预览
       # self.label = EQLabel(self)
       # #self.label.mousePress.connect(lambda:(self.label.setVisible(False) or self.browser.page().mainFrame().evaluateJavaScript("setFlagTrue()")))
       # self.label.move(70,178)
       # self.label.resize(640,330)
       # #QApplication.desktop().screenGeometry().width()
       # width =640
       # height =330
       # self.cvCamera = CvCamera(self.label,width,height)
       # self.cvCamera.stopFlowSignal.connect(self.stopFlow)
       # self.cvCamera.playVideoSignal.connect(self.playVideo)
       #self.cvCamera.startRecord(640,480)

       #指定打开界面的 URL
       self.browser.page().mainFrame().javaScriptWindowObjectCleared.connect(self.addObject)
       #self.browser.page().mainFrame().evaluateJavaScript()
       #print("file:///" + QFileInfo("UI/Page/NewUI/standby.html").absoluteFilePath())
       self.browser.load(QUrl("file:///" + QFileInfo("UI/Page/Login.html").absoluteFilePath()))
       #self.browser.load(QUrl("file:////E:\VSProjects2017\TY_75Stability_Firecracker\TY_75Stability.RPIApp/UI/Page/Login.html"))
       #self.browser.load(QUrl("http://mint-ui.github.io/#!/zh-cn"))



    #将Python对象注入到JavaScript
    def addObject(self):
        self.sysControl = SysControl()
        self.sysControl.closeFormSignal.connect(self.close)
        self.browser.page().mainFrame().addToJavaScriptWindowObject("sys",self.sysControl)

    #窗体关闭前执行
    def closeEvent(self,QCloseEvent):
        time.sleep(0.1)

    def evalJs(self,js):
        self.browser.page().mainFrame().evaluateJavaScript(js)
        
    def stopFlow(self):
        self.browser.page().mainFrame().evaluateJavaScript("stopFlow()")

    def playVideo(self,img):
        self.label.setPixmap(img)
  
if __name__ == "__main__":
    cgitb.enable(format ='text')
    def excepthook(type, value, trace):  
        '''''write the unhandle exception to log'''  
        print('Unhandled Error: %s: %s'%(str(type), str(value)))
        sys.__excepthook__(type, value, trace)  
    sys.excepthook = excepthook  
    print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " 启动窗体 ")
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    #mainWindow.showFullScreen()
    mainWindow.show()
    print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " 已启动 ")
    sys.exit(app.exec_())
    print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " 销毁窗体 ")
    del mainWindow
    del app

    
    
    
    

       
       
 


